package com.example.demo.leetcode.top;

/**
 *  1 2 3 4 5 null
 *
 *  1 2 null
 */
public class Btree_删除链表中倒数第N个节点 {

    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(head.next == null){
            return null;
        }

        ListNode p = new ListNode(Integer.MAX_VALUE);
        p.next = head;
        ListNode q = head;
        for(int i=0;i<n;i++){
            q = q.next;
        }
        while(q!=null){
            p = p.next;
            q = q.next;
        }
        p.next = p.next.next;
        if(p.val == Integer.MAX_VALUE){
            return p.next;
        }
        return head;
    }

      public class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }

}
